package com.situ.stl.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.stl.model.Reserve;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ReserveMapper extends BaseMapper<Reserve> {

    // 批量更新预约状态，例如已经预约或者已经取消
    @Update("<script>" +
            "update reserve set status = #{status} " +
            "where id in " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int batchUpdateStatus(@Param("status") Integer status, @Param("ids") List<Integer> ids);

    // 根据ID查询单个预约（包含关联信息）
    @Select("select r.*, c.*, m.* " +
            "from reserve r " +
            "left join course c on r.course_id = c.id " +
            "left join member m on r.member_id = m.id " +
            "where r.id = #{id}")
    @Results({
            @Result(column = "id", property = "id"),
            // 课程关联（完整映射）
            @Result(column = "course_id", property = "course",
                    one = @One(select = "com.situ.stl.dao.CourseMapper.selectById")),
            // 会员关联（完整映射）
            @Result(column = "member_id", property = "member",
                    one = @One(select = "com.situ.stl.dao.MemberMapper.selectById"))
    })
    Reserve selectWithRelations(Integer id);

    // 检查会员是否预约课程
    @Select("select * from reserve where member_id = #{memberId} and course_id = #{courseId}")
    Reserve selectByMemberAndCourse(@Param("memberId") Integer memberId, @Param("courseId") Integer courseId);


    @Select("select r.*, c.name as course_name, c.time as course_time, \n" +
            "m.name as member_name, m.phone as member_phone, ch.name as coach_name \n" +
            "from reserve r \n" +
            "left join course c on r.course_id = c.id \n" +
            "left join member m on r.member_id = m.id \n" +
            "left join coach ch on c.coach_id = ch.id \n" +
            "${ew.customSqlSegment}")



    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "course_id", property = "course", one = @One(select = "com.situ.stl.dao.CourseMapper.selectById")),
            @Result(column = "member_id", property = "member", one = @One(select = "com.situ.stl.dao.MemberMapper.selectById")),
            @Result(column = "member_name", property = "memberName"),
            @Result(column = "course_name", property = "courseName"),
            @Result(column = "coach_name", property = "coachName")
    })
    Page<Reserve> selectPageWithRelations(Page<Reserve> page, @Param(Constants.WRAPPER) Wrapper<Reserve> queryWrapper);


    // 多条件关联查询（非分页），用于/search接口
    @Select("select r.*, " +
            "m.name as member_name, " +  // 会员姓名
            "c.name as course_name, " +  // 课程名称
            "ch.name as coach_name " +   // 教练名称
            "from reserve r " +
            "left join member m on r.member_id = m.id " +  // 关联会员表
            "left join course c on r.course_id = c.id " +  // 关联课程表
            "left join coach ch on c.coach_id = ch.id " +
            "${ew.customSqlSegment}")  // 支持动态条件
    @Results({
            @Result(column = "id", property = "id", id = true),
            // 映射预约表基础字段
            @Result(column = "status", property = "status"),
            @Result(column = "score", property = "score"),
            @Result(column = "comment", property = "comment"),
            @Result(column = "comment_time", property = "commentTime"),
            @Result(column = "reserve_time", property = "reserveTime"),
            @Result(column = "course_id", property = "courseId"),
            @Result(column = "member_id", property = "memberId"),
            // 映射关联名称字段
            @Result(column = "member_name", property = "memberName"),
            @Result(column = "course_name", property = "courseName"),
            @Result(column = "coach_name", property = "coachName")
    })
    List<Reserve> selectWithRelationsList(@Param(Constants.WRAPPER) Wrapper<Reserve> queryWrapper);


    // 查询会员的所有预约
    @Select("select * from reserve where member_id = #{memberId} order by reserve_time desc")
    List<Reserve> selectByMember(Integer memberId);

    // 查询课程的所有预约
    @Select("select r.*, m.id as member_id, m.name as member_name, c.name as course_name, ch.name as coach_name " +
            "from reserve r " +
            "left join member m on r.member_id = m.id " +
            "left join course c on r.course_id = c.id " +
            "left join coach ch on c.coach_id = ch.id " +
            "where r.course_id = #{courseId} " +
            "order by r.reserve_time desc")
    @Results({
            @Result(column = "id", property = "id", id = true),
            // 映射预约表字段
            @Result(column = "status", property = "status"),
            @Result(column = "reserve_time", property = "reserveTime"),
            @Result(column = "course_id", property = "courseId"),
            // 关联映射会员信息
            @Result(column = "member_id", property = "member.id"),
            @Result(column = "member_name", property = "memberName"),
            @Result(column = "course_name", property = "courseName"),
            @Result(column = "coach_name", property = "coachName")
    })
    List<Reserve> selectByCourse(Integer courseId);

    // 通过会员姓名和课程名称查询预约
    @Select("select r.*, m.name as member_name, c.name as course_name, ch.name as coach_name " +
            "from reserve r " +
            "left join member m on r.member_id = m.id " +
            "left join course c on r.course_id = c.id " +
            "left join coach ch on c.coach_id = ch.id " +
            "where m.name like concat('%', #{memberName}, '%') " +
            "and c.name like concat('%', #{courseName}, '%')")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "course_id", property = "course", one = @One(select = "com.situ.stl.dao.CourseMapper.selectById")),
            @Result(column = "member_id", property = "member", one = @One(select = "com.situ.stl.dao.MemberMapper.selectById")),
            @Result(column = "member_name", property = "memberName"),
            @Result(column = "course_name", property = "courseName"),
            @Result(column = "coach_name", property = "coachName")
    })
    List<Reserve> selectByMemberNameAndCourseName(@Param("memberName") String memberName, @Param("courseName") String courseName);
}

